package com.bocai.kelalacar.ui.car.set.act;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.bocai.kelalacar.App;
import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseActivity;
import com.bocai.kelalacar.ui.car.main.act.MainActivity;
import com.bocai.kelalacar.ui.car.set.contract.PwSetContract;
import com.bocai.kelalacar.ui.car.set.module.PwSetModule;
import com.bocai.kelalacar.ui.car.set.presenter.PwSetPresenter;
import com.bocai.kelalacar.util.SP;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yuanfei on 2017/6/20.
 */

public class PwSetSecondActivity extends BaseActivity implements PwSetContract.view {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.et_first_psd)
    EditText etFirstPsd;
    @BindView(R.id.et_second_psd)
    EditText etSecondPsd;
    @BindView(R.id.btn_next)
    Button btnNext;
    private String code;
    private String phone;
    @Inject
    PwSetPresenter presenter;
    @BindView(R.id.ll_back)
    LinearLayout llBack;


    @Override
    public int getLayoutId() {
        return R.layout.act_pw_set_second;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new PwSetModule(this)).inject(this);
    }

    @Override
    public void initView() {
        code = getIntent().getStringExtra("code");
        phone = getIntent().getStringExtra("phone");
        initToolbar("密码设置");
        initBack(imgBack,llBack);
        etFirstPsd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (etFirstPsd.getText().toString().trim().length() > 5&&etSecondPsd.getText().toString().trim().length() > 5) {
                    btnNext.setEnabled(true);
                    btnNext.setTextColor(getResources().getColor(R.color.color_white));
                } else {
                    btnNext.setEnabled(false);
                    btnNext.setTextColor(getResources().getColor(R.color.text_color));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        etSecondPsd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (etFirstPsd.getText().toString().trim().length() > 5&&etSecondPsd.getText().toString().trim().length() > 5) {
                    btnNext.setEnabled(true);
                } else {
                    btnNext.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
    @OnClick({R.id.btn_next})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btn_next:
                if (etFirstPsd.getText().toString().trim().equals(etSecondPsd.getText().toString().trim())) {

                    presenter.resetPw(phone, etFirstPsd.getText().toString().trim(), code, SP.get(this,"MemberType","").toString());
                }else {
                    showShortToast("两次密码不一致");
                }
                break;
        }
    }

    @Override
    public void getCode(String code) {

    }

    @Override
    public void resetPw() {
        showShortToast("密码设置成功");
        startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
        finish();
    }

    @Override
    public void setBtnCode(int index) {

    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        showShortToast(msg);
        if (msg.equals("您的登录已失效，请重新登录")){
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }
    }
}
